package com.demo.paypalmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.paypalmanagement.common.ResultMsg;
import com.demo.paypalmanagement.config.response.BizException;
import com.demo.paypalmanagement.model.entity.Products;
import com.demo.paypalmanagement.model.vo.ProductsVO;
import com.demo.paypalmanagement.service.IProductsService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("com/products")
@RestController
public class UsProductsController {

  @Autowired
  private IProductsService productsService;
  @GetMapping("/page")
  public IPage<Products> productsIPage(ProductsVO vo) throws InterruptedException {
    if (ObjectUtils.isEmpty(vo)) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }
    Page<Products> page = new Page<>(vo.getPageNo(), vo.getPageSize());
    QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
    queryWrapper.lambda()
            .ne(Products::getIsActive,0)
//            .ne(Products::getIsPrize,1)
            .like(StringUtils.isNotBlank(vo.getName()),Products::getName,vo.getName())
            .eq(ObjectUtils.isNotEmpty(vo.getIsPrize()),Products::getIsPrize,vo.getIsPrize())
            .eq(ObjectUtils.isNotEmpty(vo.getIsActive()),Products::getIsActive, vo.getIsActive())
            .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()),Products::getCreatedAt,vo.getBeginDate(),vo.getEndDate());
    return productsService.page(page, queryWrapper);
  }
}
